<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <meta charset="UTF-8">
    <title>员工管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" th:href="@{/favicon.ico}">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/layui_ext/dtree/dtree.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/layui_ext/dtree/font/dtreefont.css}" media="all"/>
    <style>
        .layui-elem-quote .layui-form .layui-form-item .layui-input-inline {
            width: 150px;
        }
    </style>
</head>
<body class="childrenBody" sec:authorize="hasPermission('/admin/user/staffList',null)">
<!-- 查询条件开始 -->
<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 80px">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input"  style="width: 150px">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 80px">账号名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" autocomplete="off" class="layui-input" style="width: 150px">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 90px">所属城市</label>
                <div class="layui-input-inline">
                    <select name="city" id="search_city" lay-search="">
                        <option value="">请选择城市</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 80px">职位</label>
                <div class="layui-input-inline">
                    <input type="text" name="position" autocomplete="off" class="layui-input" style="width: 150px">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="doSearch"><span
                        class="layui-icon layui-icon-search"></span>查询
                </button>
                <button type="reset" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </form>
</blockquote>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="staffTable" lay-filter="staffTable"></table>
    <div id="staffToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm" sec:authorize="hasPermission('/admin/user/addStaff','staff:add')">
            <span class="layui-icon layui-icon-add-1"></span>添加员工
        </button>
    </div>

    <div id="staffRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm" sec:authorize="hasPermission('/admin/user/updateStaff','staff:update')">
            <span class="layui-icon layui-icon-edit"></span>编辑
        </button>
        <button type="button" lay-event="selectRole" class="layui-btn layui-btn-sm layui-btn-normal" sec:authorize="hasPermission('/admin/user/saveStaffRole','staff:save')">
            <span class="layui-icon layui-icon-user"></span>分配角色
        </button>
        <button type="button" lay-event="reset" class="layui-btn layui-btn-sm layui-btn-warm" sec:authorize="hasPermission('/admin/user/resetPwd','staff:reset')"><span
                class="layui-icon layui-icon-edit"></span>重置密码
        </button>
    </div>
</div>

<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="addOrUpdateDiv" sec:authorize="hasPermission('/admin/user/addStaff','staff:add') || hasPermission('/admin/user/updateStaff','staff:update')">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="id">
                    <input type="hidden" name="type">
                    <input type="text" name="name" id="name" lay-verify="required" autocomplete="off"
                           placeholder="请输入姓名" class="layui-input" maxlength="20">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">账号名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" id="username" lay-verify="required" autocomplete="off"
                           placeholder="请输入账号名" class="layui-input" maxlength="20">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-inline">
                    <input type="tel" name="phone" id="phone" lay-verify="required|phone" autocomplete="off"
                           placeholder="请输入手机号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">身份证号</label>
                <div class="layui-input-inline">
                    <input type="text" name="identity" id="identity" lay-verify="required|identity" autocomplete="off"
                           placeholder="请输入身份证号" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">城市</label>
                <div class="layui-input-inline">
                    <select name="city" id="city" lay-search="" lay-verify="required">
                        <option value="">请选择城市</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">职位</label>
                <div class="layui-input-inline">
                    <input type="text" name="position" id="position" lay-verify="required" autocomplete="off"
                           placeholder="请输入职位" class="layui-input" maxlength="20" value="员工" readonly>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="1" title="男" checked="">
                    <input type="radio" name="sex" value="0" title="女">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">是否可用</label>
                <div class="layui-input-inline">
                    <input type="radio" name="available" value="1" title="可用" checked="">
                    <input type="radio" name="available" value="0" title="不可用">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit"><span
                        class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->


<!-- 员工分配角色弹出层  开始 -->
<div style="display: none;padding: 5px" id="selectStaffRoleDiv" sec:authorize="hasPermission('/admin/user/saveStaffRole','staff:save')">
    <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
</div>


<!-- 员工分配角色弹出层  结束 -->


<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
<script type="text/javascript">
    var tableIns;
    layui.extend({
        dtree: '/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery', 'form', 'table', 'layer', 'dtree'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var dtree = layui.dtree;
        //加载 数据
        tableIns = table.render({
            elem: '#staffTable'
            , url: '/admin/user/loadAllStaff'
            , toolbar: '#staffToolBar' //开启头部工具栏，并为其绑定左侧模板
            , title: '员工数据表'
            , page: true
            , cols: [ [
                {field: 'name', title: '姓名', align: 'center', width: '120'}
                , {field: 'username', title: '帐号', align: 'center', width: '150'}
                , {
                    field: 'sex', title: '性别', align: 'center', width: '60', templet: function (d) {
                        return d.sex === 1 ? '<span style="color: blue">男</span>' : '<span style="color: red">女</span>';
                    }
                }
                , {field: 'position', title: '职位', align: 'center', width: '150'}
                , {field: 'c_name', title: '所属城市', align: 'center', width: '110',sort: true}
                , {
                    field: 'available', title: '是否可用', align: 'center', width: '90', templet: function (d) {
                        return d.available == 1 ? '<span style="color: blue">可用</span>' : '<span style="color: red">不可用</font>';
                    }
                }
                , {field: 'phone', title: '手机号', align: 'center', width: '120'}
                , {field: 'identity', title: '身份证', align: 'center', width: '180'}
                , {fixed: 'right', title: '操作', toolbar: '#staffRowBar', align: 'center', width: '320'}
            ] ]
            , done: function (res, curr, count) { //处理删除某一页最后一条数据的BUG
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            }
        });

        $.ajax({
            url: "/admin/city/getCity2",
            success: function (res){
                var citys = res.data;
                var html = "<option value=''>请选择城市</option>";
                $.each(citys, function (index, item) {
                    html += "<option value='" + item.id + "'>" + item.name + "</option>";
                });
                $("#search_city").html(html);
                form.render("select");
            },
            dataType: "json"
        });

        //模糊查询
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监听工具条的事件
        table.on("toolbar(staffTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddLayer();
                    break;
            }
            ;
        });

        //监听行工具条的事件
        table.on("tool(staffTable)", function (obj) {
            var data = obj.data; //获得当前行数据
            switch (obj.event) {
                case 'update':
                    openUpdateStaffLayer(data);
                    break;
                case 'reset':
                    resetPwd(data);
                    break;
                case 'selectRole':
                    selectRole(data);
                    break;
            }
            ;
        });

        var mainIndex;
        var url;

        //打开添加的弹出层
        function openAddLayer() {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '550px'],
                title: '添加员工',
                success: function () {
                    $("#dataFrm")[0].reset();
                    url = "/admin/user/addStaff";
                    $.ajax({
                        url: "/admin/city/getCity1",
                        success: function (res){
                            var citys = res.data;
                            var html = "<option value=''>请选择城市</option>";
                            $.each(citys, function (index, item) {
                                html += "<option value='" + item.id + "'>" + item.name + "</option>";
                            });
                            $("#city").html(html);
                            form.render("select");
                        },
                        dataType: "json"
                    });
                    $('#username').removeAttr('disabled');
                }
            });
        }

        //打开修改的弹出层
        function openUpdateStaffLayer(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '550px'],
                title: '修改员工',
                success: function () {
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm", data);
                    var city = data.city;
                    //选中城市
                    $.ajax({
                        url: "/admin/city/getCity1",
                        success: function (res){
                            var citys = res.data;
                            var dom_mgr=$("#city");
                            var html = "<option value=''>请选择城市</option>";
                            $.each(citys, function (index, item) {
                                html += "<option value='" + item.id + "'>" + item.name + "</option>";
                            });
                            $("#city").html(html);
                            //选中一个
                            dom_mgr.val(city);
                            //重新渲染
                            form.render("select");
                        },
                        dataType: "json"
                    });
                    $('#username').attr('disabled', 'disabled')
                    url = "/admin/user/updateStaff";
                }
            });
        }

        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (res) {
                if (res.code == 200) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
            return false;
        })

        //重置密码
        function resetPwd(data) {
            layer.confirm('你确定要重置【' + data.name + '】这个员工密码吗?', {icon: 3, title: '提示'}, function (index) {
                $.post("/admin/user/resetPwd", {id: data.id}, function (res) {
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

        //打开分配角色的弹出层
        function selectRole(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#selectStaffRoleDiv"),
                area: ['800px', '600px'],
                title: '分配【' + data.name + '】的角色',
                btn: ['<span class=layui-icon>&#xe605;确认分配</span>', '<span class=layui-icon>&#x1006;关闭窗口</span>'],
                yes: function (index, layero) {
                    var checkStatus = table.checkStatus('roleTable');
                    var params = "id=" + data.id;
                    $.each(checkStatus.data, function (index, item) {
                        params += "&ids=" + item.id;
                    });
                    $.post("/admin/user/saveStaffRole", params, function (res) {
                        layer.msg(res.msg);
                    })
                    layer.close(index);
                },
                btn2: function (index, layero) {
                    //return false 开启该代码可禁止点击该按钮关闭
                },
                btnAlign: 'c',
                success: function () {
                    initRoleTable(data);
                }
            });
        }

        var roleTableIns;

        //初始化角色列表
        function initRoleTable(data) {
            roleTableIns = table.render({
                elem: '#roleTable'
                , url: '/admin/user/initStaffRole'
                , where: {
                    id: data.id
                }
                , cols: [ [
                    {type: 'checkbox', align: 'center'}
                    , {field: 'id', title: 'ID', align: 'center'}
                    , {field: 'name', title: '角色名称', align: 'center'}
                    , {field:'nameZh', title:'中文名称',align:'center'}
                    , {field: 'remark', title: '角色备注', align: 'center'}
                ] ]
            });
        }
    });
</script>

</body>
</html>